package com.ruoyi.guarantee.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.guarantee.domain.BBiddingQy;
import com.ruoyi.guarantee.mapper.BBiddingMapper;
import com.ruoyi.guarantee.domain.BBidding;
import com.ruoyi.guarantee.service.IBBiddingService;

/**
 * 投标保函Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-05
 */
@Service
public class BBiddingServiceImpl implements IBBiddingService 
{
    @Autowired
    private BBiddingMapper bBiddingMapper;

    /**
     * 查询投标保函
     * 
     * @param id 投标保函主键
     * @return 投标保函
     */
    @Override
    public BBidding selectBBiddingById(Long id)
    {
        BBidding bBidding = bBiddingMapper.selectBBiddingById(id);
        List<BBiddingQy> bBiddingQyList = bBidding.getCompanyArr();
        if (StringUtils.isNotNull(bBiddingQyList))
        {
            List<BBiddingQy> list = new ArrayList<BBiddingQy>();
            for (BBiddingQy bBiddingQy : bBiddingQyList)
            {
                if (StringUtils.isNotEmpty(bBiddingQy.getXmNameLot())){
//                    strList.toArray(new String[strList.size()]);
                    bBiddingQy.setXmNameLots(bBiddingQy.getXmNameLot().split(","));
                }
            }
        }
        return bBidding;
    }

    /**
     * 查询投标保函列表
     * 
     * @param bBidding 投标保函
     * @return 投标保函
     */
    @Override
    public List<BBidding> selectBBiddingList(BBidding bBidding)
    {
        return bBiddingMapper.selectBBiddingList(bBidding);
    }

    /**
     * 新增投标保函
     * 
     * @param bBidding 投标保函
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBBidding(BBidding bBidding)
    {
        bBidding.setCreateTime(DateUtils.getNowDate());
        int rows = bBiddingMapper.insertBBidding(bBidding);
        insertBBiddingQy(bBidding);
        return rows;
    }

    /**
     * 修改投标保函
     * 
     * @param bBidding 投标保函
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBBidding(BBidding bBidding)
    {
        bBidding.setUpdateTime(DateUtils.getNowDate());
        bBiddingMapper.deleteBBiddingQyByBidId(bBidding.getId());
        insertBBiddingQy(bBidding);
        return bBiddingMapper.updateBBidding(bBidding);
    }

    /**
     * 修改投标保函
     *
     * @param bBidding 投标保函
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBBiddingStatus(BBidding bBidding)
    {
        bBidding.setUpdateTime(DateUtils.getNowDate());
        return bBiddingMapper.updateBBidding(bBidding);
    }

    /**
     * 批量删除投标保函
     * 
     * @param ids 需要删除的投标保函主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBBiddingByIds(Long[] ids)
    {
//        bBiddingMapper.deleteBBiddingQyByBidIds(ids);
        return bBiddingMapper.deleteBBiddingByIds(ids);
    }

    /**
     * 删除投标保函信息
     * 
     * @param id 投标保函主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBBiddingById(Long id)
    {
//        bBiddingMapper.deleteBBiddingQyByBidId(id);
        return bBiddingMapper.deleteBBiddingById(id);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BBidding> selectyyzgBBiddingList(BBidding bBidding) {
        return bBiddingMapper.selectyyzgBBiddingList(bBidding);
    }

    @Override
    public List<BBidding> selectyyzBBiddingList(BBidding bBidding) {
        return bBiddingMapper.selectyyzBBiddingList(bBidding);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BBidding> selectHistoryBiddingList(BBidding bBidding) {
        return bBiddingMapper.selectHistoryBiddingList(bBidding);
    }

    /**
     * 新增投标保函-企业信息信息
     * 
     * @param bBidding 投标保函对象
     */
    public void insertBBiddingQy(BBidding bBidding)
    {
        List<BBiddingQy> bBiddingQyList = bBidding.getCompanyArr();
        Long id = bBidding.getId();
        if (StringUtils.isNotNull(bBiddingQyList))
        {
            List<BBiddingQy> list = new ArrayList<BBiddingQy>();
            for (BBiddingQy bBiddingQy : bBiddingQyList)
            {
                if (bBiddingQy.getXmNameLots().length>0){
                    bBiddingQy.setXmNameLot(String.join(",",bBiddingQy.getXmNameLots()));
                }
                bBiddingQy.setCreateBy(bBidding.getCreateBy());
                bBiddingQy.setBidId(id);
                list.add(bBiddingQy);
            }
            if (list.size() > 0)
            {
                bBiddingMapper.batchBBiddingQy(list);
            }
        }
    }
}
